import { FormSchema } from '@/components/Form'
import { useI18n } from '@/hooks/web/useI18n'
import { reactive } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'
import { useDictStore } from '@/store/modules/dictData'
import { CrudSchema } from '@/hooks/web/useCrudSchemas'
const { t } = useI18n()
const { required } = useValidator()
const DictStore = useDictStore()
export const formschema = reactive<FormSchema[]>([
  {
    field: 'siteName',
    label: '站点名称',
    component: 'Input',
    colProps: {
      span: 5
    }
  },
  {
    field: 'deviceNo',
    label: t('设备编号'),
    component: 'Input',
    colProps: {
      span: 5
    }
  },
  {
    field: 'lon',
    label: t('经度'),
    component: 'Input',
    colProps: {
      span: 4
    }
  },
  {
    field: 'lat',
    label: t('纬度'),
    component: 'Input',
    colProps: {
      span: 4
    }
  },
  {
    field: 'createTime',
    label: t('创建时间'),
    component: 'Input',
    colProps: {
      span: 5
    },
    componentProps: {
      disabled: true
    }
  },
  {
    field: 'deviceType',
    label: '设备类型',
    component: 'Select',
    colProps: {
      span: 5
    },
    componentProps: {
      options: DictStore.$state.scada_device_type?.map((item) => {
        return {
          label: item.dictLabel,
          value: item.dictValue
        }
      })
    }
  },
  {
    field: 'markerType',
    label: t('站点类型'),
    component: 'Select',
    colProps: {
      span: 5
    },
    componentProps: {
      options: [
        {
          label: '普通类型',
          value: 0
        },
        {
          label: '气源',
          value: 1
        }
      ]
    }
  },
  {
    field: 'isMonitor',
    label: t('监控页关注'),
    component: 'Switch',
    componentProps: {
      activeValue: 1,
      inactiveValue: 0
    },
    colProps: {
      span: 4
    }
  },
  {
    field: 'offSecond',
    label: t('离线时长'),
    component: 'Input',
    colProps: {
      span: 4
    }
  },
  {
    field: 'address',
    label: t('地址'),
    component: 'Input',
    colProps: {
      span: 5
    }
  }
])
export const AddFormschema = reactive<FormSchema[]>([
  {
    field: 'siteName',
    label: '站点名称',
    component: 'Input',
    colProps: {
      span: 10
    }
  },
  {
    field: 'deviceNo',
    label: t('设备编号'),
    component: 'Input',
    colProps: {
      span: 10
    }
  },
  {
    field: 'lon',
    label: t('经度'),
    component: 'Input',
    value: 0,
    colProps: {
      span: 10
    }
  },
  {
    field: 'lat',
    label: t('纬度'),
    component: 'Input',
    value: 0,
    colProps: {
      span: 10
    }
  },
  {
    field: 'deviceType',
    label: '设备类型',
    component: 'Select',
    colProps: {
      span: 10
    },
    componentProps: {
      options: DictStore.$state.scada_device_type?.map((item) => {
        return {
          label: item.dictLabel,
          value: item.dictValue
        }
      })
    }
  },
  {
    field: 'markerType',
    label: t('站点类型'),
    component: 'Select',
    colProps: {
      span: 10
    },
    value: 0,
    componentProps: {
      options: [
        {
          label: '普通类型',
          value: 0
        },
        {
          label: '气源',
          value: 1
        }
      ]
    }
  },
  {
    field: 'transmitterNo',
    label: t('变送器数量'),
    component: 'InputNumber',
    value: 1,
    colProps: {
      span: 10
    }
  },
  {
    field: 'flowmeterNo',
    label: t('流量计数量'),
    component: 'InputNumber',
    value: 1,
    colProps: {
      span: 10
    }
  },
  {
    field: 'offSecond',
    label: t('离线时长(s)'),
    value: 600,
    component: 'InputNumber',
    colProps: {
      span: 10
    }
  },
  {
    field: 'address',
    label: t('地址'),
    component: 'Input',
    colProps: {
      span: 20
    }
  },
  {
    field: 'isMonitor',
    label: t('监控页是否关注'),
    component: 'Switch',
    componentProps: {
      activeValue: 1,
      inactiveValue: 0
    },
    colProps: {
      span: 10
    }
  }
])
export const DetectorFormschema = reactive<FormSchema[]>([
  {
    field: 'devName',
    label: '设备名称',
    component: 'Input',
    colProps: {
      span: 5
    }
  },
  {
    field: 'devNo',
    label: t('设备序号'),
    component: 'Input',
    colProps: {
      span: 5
    }
  },
  {
    field: 'devType',
    label: t('设备类型'),
    component: 'Select',
    colProps: {
      span: 5
    },
    componentProps: {
      options: [
        {
          label: '流量计',
          value: 0
        },
        {
          label: '变送器',
          value: 1
        }
      ],
      on: {
        change: async (val: number) => {
          if (val == 0) {
            DetectorFormschema[8].hidden = false
          } else {
            DetectorFormschema[8].hidden = true
          }
        }
      }
    }
  },

  {
    field: 'model',
    label: t('型号'),
    component: 'Input',
    colProps: {
      span: 4
    }
  },
  {
    field: 'protName',
    label: t('协议名称'),
    component: 'Input',
    colProps: {
      span: 5
    }
  },
  {
    field: 'protCode',
    label: '协议代码',
    component: 'Input',
    colProps: {
      span: 5
    }
  },
  {
    field: 'requestAddr',
    label: t('请求地址'),
    component: 'Input',
    colProps: {
      span: 5
    }
  },
  {
    field: 'devDescribe',
    label: t('设备描述'),
    component: 'Input',
    colProps: {
      span: 5
    }
  },
  {
    field: 'isGasSum',
    label: t('是否统计用气量'),
    component: 'Switch',
    componentProps: {
      activeValue: 1,
      inactiveValue: 0
    },
    colProps: {
      span: 5
    }
  }
])
export const AddDetectorFormschema = reactive<FormSchema[]>([
  {
    field: 'devName',
    label: '设备名称',
    component: 'Input',
    colProps: {
      span: 12
    }
  },
  {
    field: 'devNo',
    label: t('设备序号'),
    component: 'Input',
    colProps: {
      span: 12
    }
  },
  {
    field: 'devType',
    label: t('设备类型'),
    component: 'Select',
    colProps: {
      span: 12
    },
    value: 0,
    componentProps: {
      options: [
        {
          label: '流量计',
          value: 0
        },
        {
          label: '变送器',
          value: 1
        }
      ],
      on: {
        change: async (val: number) => {
          if (val == 0) {
            AddDetectorFormschema[3].hidden = false
          } else {
            AddDetectorFormschema[3].hidden = true
          }
        }
      }
    }
  },
  {
    field: 'isGasSum',
    label: t('是否统计用气量'),
    component: 'Switch',
    componentProps: {
      activeValue: 1,
      inactiveValue: 0
    },
    colProps: {
      span: 19
    }
  },
  {
    field: 'model',
    label: t('型号'),
    component: 'Input',
    colProps: {
      span: 12
    }
  },
  {
    field: 'protName',
    label: t('协议名称'),
    component: 'Input',
    colProps: {
      span: 12
    }
  },
  {
    field: 'protCode',
    label: '协议代码',
    component: 'Input',
    colProps: {
      span: 12
    }
  },
  {
    field: 'requestAddr',
    label: t('请求地址'),
    component: 'Input',
    colProps: {
      span: 12
    }
  },
  {
    field: 'devDescribe',
    label: t('设备描述'),
    component: 'Input',
    colProps: {
      span: 12
    }
  }
])
export const MeasureSchemas = reactive<CrudSchema[]>([
  {
    field: 'index',
    label: t('tableDemo.index'),
    type: 'index',
    fixed: 'left',
    form: {
      hidden: true
    }
  },
  {
    field: 'name',
    label: `量测名称`,
    form: {
      hidden: false,
      colProps: {
        span: 20
      }
    },
    table: {
      hidden: false
    }
  },
  {
    field: 'affectField',
    label: `量测标识`,
    formatter: (_: Recordable, __: any, cellValue: string) => {
      return DictStore.$state.scada_meas_unit?.map((item) => {
        if (item.dictValue == cellValue) {
          return t(item.dictLabel)
        }
      })
    },
    form: {
      component: 'Select',
      componentProps: {
        options: DictStore.$state.scada_ffect_field?.map((item) => {
          return {
            label: item.dictLabel,
            value: item.dictValue
          }
        })
      }
    },
    table: {
      // hidden: true,
    }
  },
  {
    field: 'measUnit',
    label: `量测单位`,
    form: {
      component: 'Select',
      componentProps: {
        options: DictStore.$state.scada_meas_unit?.map((item) => {
          return {
            label: item.dictLabel,
            value: item.dictValue
          }
        })
      }
    },
    table: {
      hidden: false
    }
  },
  {
    field: 'highLimit',
    label: `量程上限`,
    form: {
      component: 'Input'
    }
  },
  {
    field: 'highTitle',
    label: `上限标题`,
    form: {
      component: 'Input',
      colProps: {
        span: 20
      }
    },
    table: {
      hidden: true
    }
  },
  {
    field: 'highAlert',
    label: `告警上限`,
    form: {
      component: 'Input'
    }
  },
  {
    field: 'isHighAlert',
    label: `是否启用上限`,
    formatter: (_: Recordable, __: any, cellValue: number) => {
      if (cellValue == 1) {
        return t('是')
      } else {
        return t('否')
      }
    },
    form: {
      component: 'Switch',
      componentProps: {
        activeValue: 1,
        inactiveValue: 0
      }
    }
  },
  {
    field: 'lowLimit',
    label: `量程下限`,
    form: {
      component: 'Input'
    }
  },
  {
    field: 'lowTitle',
    label: `下限标题`,
    form: {
      component: 'Input',
      colProps: {
        span: 20
      }
    },
    table: {
      hidden: true
    }
  },
  {
    field: 'lowAlert',
    label: `告警下限`,
    form: {
      component: 'Input'
    }
  },
  {
    field: 'isLowAlert',
    label: `是否启用下限`,
    formatter: (_: Recordable, __: any, cellValue: number) => {
      if (cellValue == 1) {
        return t('是')
      } else {
        return t('否')
      }
    },
    form: {
      component: 'Switch',
      componentProps: {
        activeValue: 1,
        inactiveValue: 0
      }
    }
  },
  {
    field: 'ratio',
    label: `偏移系数`,
    form: {},
    table: {
      hidden: true
    }
  },
  {
    field: 'offset',
    label: `偏移量`,
    form: {},
    table: {
      hidden: true
    }
  },
  {
    field: 'description',
    label: `描述`,
    form: {
      colProps: {
        span: 24
      }
    },
    table: {
      hidden: true
    }
  },
  {
    field: 'createTime',
    label: `创建时间`,
    form: {
      hidden: false,
      colProps: {},
      componentProps: {
        disabled: true
      }
    },
    table: {
      hidden: true
    }
  }
])
export const DetecterRules = reactive({
  devName: [required()],
  devNo: [required()],
  devType: [required()]
})
export const MarkerRules = reactive({
  siteName: [required()],
  deviceNo: [required()],
  lon: [required()],
  lat: [required()],
  deviceType: [required()],
  offSecond: [required()]
})
